package com.apes.pi.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.plugin.event.EventConst;
import com.apes.framework.plugin.event.Listener;
import com.apes.pi.model.Pipe;
import com.apes.pi.model.Receiver;
import com.apes.pi.model.ReceiverParams;
import com.apes.pi.parser.QueueCacheManager;
import com.apes.pi.repository.PipeRepository;
import com.apes.pi.repository.ReceiverRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 连接器服务类
 *
 * @author wuq
 * @create 2019-10-22 15:38
 */
@Service
public class ReceiverService extends DomainService {

    @Autowired
    ReceiverRepository receiverRepository;
    @Autowired
    PipeRepository pipeRepository;

    public Receiver create(SimpleRequest request){
        Receiver receiver = request.getO(Receiver.class);
        for (ReceiverParams item : receiver.getParams()) {
            item.setReceiver(receiver);
        }
        receiver = receiverRepository.saveAndFlush(receiver);
        receiver.setBeforePipes(request.getA("beforePipes", Pipe.class));
        receiver.setAfterPipes(request.getA("afterPipes", Pipe.class));
        receiver = save(receiver);
        SpringManager.getBean(QueueCacheManager.class).addReceiver(receiver);
        return receiver;
    }

    public Receiver save(Receiver receiver){
        String receiverId = receiver.getId();
        pipeRepository.deleteByParent(receiverId);

        // 前置拦截器
        List<Pipe> beforePipe = receiver.getBeforePipes();
        if ( beforePipe != null ) {
            beforePipe.stream().forEach( pipe -> {
                pipe.setParent(receiverId);
                pipeRepository.saveAndFlush(pipe);
            });
        }

        // 后置拦截器
        List<Pipe> afterPipe = receiver.getAfterPipes();
        if ( afterPipe!= null ) {
            afterPipe.stream().forEach( pipe -> {
                pipe.setParent(receiverId);
                pipeRepository.saveAndFlush(pipe);
            });
        }
        return receiverRepository.findOne(receiver.getId());
    }

    public Receiver update(SimpleRequest request){
        Receiver receiver = request.getO(Receiver.class);
        for (ReceiverParams item : receiver.getParams()) {
            item.setReceiver(receiver);
        }
        receiver = receiverRepository.saveAndFlush(receiver);
        receiver.setBeforePipes(request.getA("beforePipes", Pipe.class));
        receiver.setAfterPipes(request.getA("afterPipes", Pipe.class));
        receiver = save(receiver);
        SpringManager.getBean(QueueCacheManager.class).addReceiver(receiver);
        return receiver;
    }

    public String delete(SimpleRequest request){
        Receiver receiver = request.getO(Receiver.class);

        // 删除管道数据
        String receiverId = receiver.getId();
        pipeRepository.deleteByParent(receiverId);

        // 删除对列
        receiverRepository.delete(receiver);
        SpringManager.getBean(QueueCacheManager.class).removeReceiver(receiver.getId());
        return "删除成功";
    }

    @Listener(topic = EventConst.POST_LOAD_EVENT)
    public void postUpdate(SimpleRequest request) {
        Object entity = request.getData();
        if(entity instanceof Receiver){
            Receiver receiver = (Receiver) entity;
            String queueId = receiver.getId();
            List<Pipe> pipes = pipeRepository.findByParentOrderById(queueId);

            List<Pipe> beforePipes = new ArrayList<>();
            List<Pipe> afterPipes = new ArrayList<>();

            pipes.stream().forEach( pipe -> {
                if ( "before".equals(pipe.getPosition()) ) {
                    beforePipes.add(pipe);
                } else {
                    afterPipes.add(pipe);
                }
            });

            receiver.setBeforePipes(beforePipes);
            receiver.setAfterPipes(afterPipes);
        }
    }
}
